package com.dongdongshop.dao;

import com.dongdongshop.data.PageBean;
import com.dongdongshop.pojo.item.TbItem;
import io.searchbox.client.JestClient;
import io.searchbox.core.Search;
import io.searchbox.core.SearchResult;
import org.apache.commons.lang3.StringUtils;
import org.elasticsearch.index.query.MatchAllQueryBuilder;
import org.elasticsearch.index.query.MultiMatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author Eayon
 * @Date 2020/4/3 18:49
 */
@Component
public class SearchDao {

    private static final Logger log = LoggerFactory.getLogger(SearchDao.class);

    @Autowired
    private JestClient jestClient;


    public PageBean<TbItem> searchItemByKeyword(String keyword, Integer pageNum, Integer pageSize) throws IOException {
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        //判断关键字是否为空
        if(StringUtils.isBlank(keyword)){
            //为空就查询全部
            MatchAllQueryBuilder matchAllQuery = QueryBuilders.matchAllQuery();
            searchSourceBuilder.query(matchAllQuery);
        }else{
            //不为空就按照关键字查询
            MultiMatchQueryBuilder matchQueryBuilder = QueryBuilders.multiMatchQuery(keyword, "title", "sellPoint");
            searchSourceBuilder.query(matchQueryBuilder);
        }
        //分页
        searchSourceBuilder.from((pageNum - 1) * pageSize);
        searchSourceBuilder.size(pageSize);
        //高亮
        HighlightBuilder highlightBuilder = new HighlightBuilder();
        highlightBuilder.preTags("<span style='color: red'>");
        highlightBuilder.field("title");
        highlightBuilder.postTags("</span>");
        searchSourceBuilder.highlighter(highlightBuilder);
        //打印DSL语句
        log.info("执行DSL语句："+searchSourceBuilder.toString());
        //执行查询
        Search search = new Search.Builder(searchSourceBuilder.toString()).addIndex("dongdongshop").addType("item").build();
        SearchResult result = jestClient.execute(search);

        //定义一个存储查询出来数据的集合
        List<TbItem> itemList = new ArrayList<>();
        //获取查询数据
        List<SearchResult.Hit<TbItem, Void>> hits = result.getHits(TbItem.class);
        for (SearchResult.Hit<TbItem, Void> hit : hits) {
            TbItem item = hit.source;
            //获取生成的高亮文本
            Map<String, List<String>> highlight = hit.highlight;
            //高亮替换
            if(highlight!=null){
                String title = highlight.get("title").get(0);
                item.setTitle(title);
            }
            //添加到返回的集合中
            itemList.add(item);
        }

        //将查询出来的数据结果集存入pageBean中
        PageBean<TbItem> pageBean = new PageBean<>();
        pageBean.setResult(itemList);//查询当前页数据
        pageBean.setPageNumber(pageNum);//当前页数
        pageBean.setPageSize(pageSize);//每页条数
        pageBean.setTotalCount(hits.size());//总条数
        pageBean.setTotalPage(pageBean.getTotalPage());//总页数
        return pageBean;
    }
}
